package com.itbaizhan.shopping_seckill_customer_api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.Orders;
import com.itbaizhan.shopping_common.pojo.SeckillGoods;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.OrderService;
import com.itbaizhan.shopping_common.service.SeckillService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/seckillGoods")
public class SeckillGoodsController {

    @DubboReference
    private SeckillService seckillService;

    @GetMapping("/findPage")
    public BaseResult<Page<SeckillGoods>> findPage(Integer page, Integer size){
        Page<SeckillGoods> pageData = seckillService.search(page, size);
        return BaseResult.ok(pageData);
    }

    @GetMapping("/findById")
    public BaseResult<SeckillGoods> findById(Long id){
        SeckillGoods seckillGoods = seckillService.findById(id);
        return BaseResult.ok(seckillGoods);
    }

    @PostMapping("/add")
    public BaseResult<Orders> add(@RequestBody Orders orders,@RequestHeader("userId") Long userId) {
        orders.setUserId(userId);
        Orders order = seckillService.createOrder(orders);
        return BaseResult.ok(order);
    }

    @GetMapping("/findOrder")
    public BaseResult<Orders> findOrder(String id){
        Orders order = seckillService.findOrder(id);
        return BaseResult.ok(order);
    }
    @DubboReference
    private OrderService orderService;
    @GetMapping("/pay")
    public BaseResult<Orders> pay(String id) {
        Orders order = seckillService.pay(id);
        orderService.add(order);
        return BaseResult.ok(order);
    }
}
